''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class Invoice
    Private WithEvents _aMetaDataTableChild As CommonExt.MetaDataTable
    Private WithEvents _aMetaDataTable As CommonExt.MetaDataTable
    Private WithEvents _aDataset As New DataSet("ReportDataSet")
    Private _aQueryName As String
    Public Property ParamOrderNo() As String


    Public Property IsGO() As Boolean



    Private Sub SalesOrder_BeforePrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeforePrint


        'Section 1: assigning data per report section
        If Not String.IsNullOrEmpty(ParamOrderNo) Then
            'Get MetaData by QueryName
            _aQueryName = "RptSO"  'Report Header
            Dim _aQueryNameChild As String = "RptSODetail" 'Report Detail for SO
            Dim _aQueryNameChild2 As String = "RptTimeDetail" 'Report Detail for Time detail
            Dim _aQueryNameChild3 As String = "RptInvoice" '. Report ?????
            Dim mq As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryName)
            Dim mqChild As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryNameChild)
            Dim mqChild2 As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryNameChild2)
            Dim mqChild3 As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryNameChild3)

            'Set Where Clause to add Parameters Value 
            'Parameter is the OrderNO 
            mq.SQLWhere = String.Format("OrderNo = '{0}'", ParamOrderNo)
            mq.FillResultQuery()

            mqChild.SQLWhere = String.Format("OrderNo = '{0}'", ParamOrderNo)
            mqChild.FillResultQuery()

            mqChild2.SQLWhere = String.Format("OrderNo = '{0}'", ParamOrderNo)
            mqChild2.FillResultQuery()

            mqChild3.SQLWhere = String.Format("OrderNo = '{0}'", ParamOrderNo)
            mqChild3.FillResultQuery()


            'Add DataTables to Dataset
            _aDataset = Nothing
            _aDataset = New DataSet("ReportDataSet")
            _aDataset.Tables.Add(mq.ResultQuery)
            _aDataset.Tables.Add(mqChild.ResultQuery)
            _aDataset.Tables.Add(mqChild2.ResultQuery)
            _aDataset.Tables.Add(mqChild3.ResultQuery)



            'Section 2 : define DataRelations 

            Dim aParentRelationField As String = mq.GUIDField 'TestPoint: SalesorderId
            Dim aChildRelationField As String = mq.GUIDField + "_FK" 'TestPoint: SalesorderId_FK

            'Between RptSO & RptSODetail
            Dim aRelation As New DataRelation("RptSO/RptSODetail", mq.ResultQuery.Columns(aParentRelationField), mqChild.ResultQuery.Columns(aChildRelationField))
            _aDataset.Relations.Add(aRelation)

            'Between RptSO & RptTimeDetail
            aRelation = New DataRelation("RptSO/RptTimeDetail", mq.ResultQuery.Columns(aParentRelationField), mqChild2.ResultQuery.Columns(aChildRelationField))
            _aDataset.Relations.Add(aRelation)

            'Between RptSO & RptInvoice
            aRelation = New DataRelation("RptSO/RptInvoice", mq.ResultQuery.Columns(aParentRelationField), mqChild3.ResultQuery.Columns(aChildRelationField))
            _aDataset.Relations.Add(aRelation)

            'Section 3 : Report field databinding
            Dim b As DevExpress.XtraReports.UI.XRBinding

            'Bind Report's Header Controls
            For Each aCol As DataColumn In mq.ResultQuery.Columns
                b = New DevExpress.XtraReports.UI.XRBinding("Text", mq.ResultQuery, aCol.ColumnName)
                Dim aControl As DevExpress.XtraReports.UI.XRControl = CType(Me.FindControl(aCol.ColumnName, True), DevExpress.XtraReports.UI.XRControl)


                If Not aControl Is Nothing Then
                    Try
                        'NOTE: This is a BIG PATCH to get the Invoice out! B.Desjardins
                        ' This should all be driven by the metacolumn property formatting string

                        If aControl.Text.Equals("Issue") Then
                            aControl.DataBindings.Add(New DevExpress.XtraReports.UI.XRBinding("Text", mq.ResultQuery, aCol.ColumnName, "{0:-#}"))
                        Else
                            aControl.DataBindings.Add(New DevExpress.XtraReports.UI.XRBinding("Text", mq.ResultQuery, aCol.ColumnName))
                        End If

                    Catch ex As Exception
                    End Try
                End If
            Next


            'Bind Sub Report Sales Order Detail ref: QN: RptSODetail
            'Desgin Patern:
            'a)Prefix all Report table that include data with "Dat_"
            'b) all CelLs to Databind , assign Tag property 
            Dim aXRTablerow As DevExpress.XtraReports.UI.XRTableRowCollection = Dat_RptSODetail.Rows
            For Each arow As DevExpress.XtraReports.UI.XRTableRow In aXRTablerow
                For Each aCell As DevExpress.XtraReports.UI.XRTableCell In arow
                    'Testpoint: Trace.WriteLine(aCell.Text)
                    If aCell.Tag.ToString.Equals("Data") Then
                        aCell.DataBindings.Add(New DevExpress.XtraReports.UI.XRBinding("Text", mqChild.ResultQuery, aCell.Text))
                    End If
                Next
            Next


            ''Bind Sub Report Time Detail ref : QN: RptTimeDetail
            aXRTablerow = Dat_RptTimeDetail.Rows
            For Each arow As DevExpress.XtraReports.UI.XRTableRow In aXRTablerow
                For Each aCell As DevExpress.XtraReports.UI.XRTableCell In arow
                    'Testpoint: Trace.WriteLine(aCell.Text)
                    If aCell.Tag.ToString.Equals("Data") Then
                        aCell.DataBindings.Add(New DevExpress.XtraReports.UI.XRBinding("Text", mqChild2.ResultQuery, aCell.Text))
                    End If

                Next
            Next


            ''Bind Sub Report Invoice  ref : QN: RptInvoice
            aXRTablerow = Dat_RptInvoice.Rows
            For Each arow As DevExpress.XtraReports.UI.XRTableRow In aXRTablerow
                For Each aCell As DevExpress.XtraReports.UI.XRTableCell In arow
                    'Testpoint: Trace.WriteLine(aCell.Text)
                    If aCell.Tag.ToString.Equals("Data") Then
                        aCell.DataBindings.Add(New DevExpress.XtraReports.UI.XRBinding("Text", mqChild3.ResultQuery, aCell.Text))
                    End If

                Next
            Next


            Me.DataSource = _aDataset
            Me.DataMember = _aDataset.Tables(0).TableName

            Me.SaleOrderDetail.DataSource = _aDataset
            Me.SaleOrderDetail.DataMember = _aDataset.Tables(1).TableName

            Me.TimeDetail.DataSource = _aDataset
            Me.TimeDetail.DataMember = _aDataset.Tables(2).TableName



            If (IsGO) Then
                imgLogo.ImageUrl = "http://www.scief.com/logo/LogoGO.jpg"
            End If


        Else
            Me.RequestParameters = True
        End If


    End Sub


    Private Sub NotesRow_BeforePrint(sender As Object, e As System.Drawing.Printing.PrintEventArgs) Handles NotesRow.BeforePrint
        If String.IsNullOrEmpty(Notes.Text) Then
            NotesRow.Visible = False
        Else
            NotesRow.Visible = True
        End If
    End Sub

    ''' <summary>
    ''' this a trick to prevent header to print on next page when no more detail to print
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TimeDetail_DataSourceRowChanged(sender As System.Object, e As DevExpress.XtraReports.UI.DataSourceRowEventArgs) Handles TimeDetail.DataSourceRowChanged
        'Howto : prevent header to print on next page when no more detail to print
        If e.CurrentRow = e.RowCount - 1 Then
            GroupHeader2.Visible = False
        End If

    End Sub

    Private Sub RefPORow_BeforePrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles RefPORow.BeforePrint
        If String.IsNullOrEmpty(PurchaseOrderNumber.Text) Then
            RefPORow.Visible = False
        Else
            RefPORow.Visible = True
        End If
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Me.Name = MetaString.GetMetaStringByCode("CAP-000128", AppManager.ApplicationOption.LanguageId).DefaultText

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class